import FormDefinitionHelper from '@/components/form/FromDesigner/FormDefinitionHelper';
import {isEmpty, isNotEmpty} from '@/utils/CommonUtils';
import {DefaultFormDefinition, DefaultMenuActionRange} from '@/components/form/DefaultFormDefinition';


export default class {
  formDefinition;
  menuActionRanges;
  formDefinitionHelper;
  menuDefinition;

  constructor(formDefinition, menuActionRanges, menuDefinition) {
    this.formDefinition = formDefinition;
    this.menuActionRanges = menuActionRanges;
    this.menuDefinition = menuDefinition;
    this.formDefinitionHelper = new FormDefinitionHelper(this.formDefinition);
  }

  getMainFields(mainFields) {
    mainFields.splice(0, mainFields.length, ...this.formDefinitionHelper.getMainFields());
  }

  initMenuActionRanges() {
    this.initDefaultValues();
    this.parseconditionalOptions();
  }

  parseconditionalOptions() {
    let menuActionRanges = this.menuDefinition.value.menuActionRanges;
    menuActionRanges.forEach(range => {
      this.parseActionOption(range);
    });

    if (isNotEmpty(menuActionRanges)) {
      this.menuActionRanges.value = menuActionRanges;
    }
  }

  parseActionOption(range) {
    range.conditionalOptions = [];
    let fieldDefinition = this.formDefinitionHelper.findFieldsByName(range.fieldName)
    if(isEmpty(fieldDefinition)) return;
    range.fieldComponent = fieldDefinition.component;

    if (isNotEmpty(range.fieldComponent) && isNotEmpty(conditionMap[range.fieldComponent])) {
      range.conditionalOptions = conditionMap[range.fieldComponent];
    }
  }

  initDefaultValues() {
    if (isEmpty(this.menuDefinition.value.menuActionRanges)) {
      this.menuDefinition.value.menuActionRanges = [JSON.parse(JSON.stringify(DefaultMenuActionRange))];
    }

    this.menuActionRanges.value = this.menuDefinition.value.menuActionRanges;
    if (isEmpty(this.formDefinition)) {
      this.formDefinition = DefaultFormDefinition;
    }
  }

  getFieldByFieldName(fieldName) {
    return this.formDefinitionHelper.findFieldsByName(fieldName);
  }
}

const conditionMap = {
  TextInput: [
    {title: '等于', value: '='},
    {title: '不等于', value: '!='},
    {title: '包含', value: 'like'},
  ],
  TextAreaInput: [
    {title: '等于', value: '='},
    {title: '不等于', value: '!='},
    {title: '包含', value: 'like'},
  ],
  FlowStatus: [
    {title: '等于', value: '='},
    {title: '不等于', value: '!='}
  ],
  EnumInput: [
    {title: '等于', value: '='},
    {title: '不等于', value: '!='}
  ],
  RadioInput: [
    {title: '等于', value: '='},
    {title: '不等于', value: '!='}
  ],
  NumberInput: [
    {title: '等于', value: '='},
    {title: '大于', value: '>'},
    {title: '小于', value: '<'},
    {title: '大于等于', value: '>='},
    {title: '小于等于', value: '<='},
  ],
  DateInput: [
    {title: '等于', value: '='},
    {title: '大于', value: '>'},
    {title: '小于', value: '<'},
    {title: '大于等于', value: '>='},
    {title: '小于等于', value: '<='},
  ],
  TimeInput: [
    {title: '等于', value: '='},
    {title: '大于', value: '>'},
    {title: '小于', value: '<'},
    {title: '大于等于', value: '>='},
    {title: '小于等于', value: '<='},
  ],
  CheckInput: [
    {title: '等于', value: '='},
  ],
  UserInput: [
    {title: '等于', value: '='},
  ],
  DepartInput: [
    {title: '等于', value: '='},
  ],
  UnitInput: [
    {title: '等于', value: '='},
  ]
};
